<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class AdmPage extends Controller {
	var $data;
	
	function AdmPage()
	{
		parent::Controller();

		$this-> db-> query('SET NAMES utf8');
		header("Content-Type: text/html; charset=UTF-8"); 

		$this->form_validation->set_message('signup', 'Введены неверные данные');
		
		$this->lang->load('main');
		$this->load->model('page','',TRUE);
		$this->load->model('menu','',TRUE);
		
		$this->data['lang'] = $this->lang;
		//
		//Conf
		$q = $this->db->get('conf');
		if ($q -> num_rows() > 0) {
			foreach ($q -> result() as $row) {
				$conf[$row->var] = $row->val;
			}
		}
		$this->data['conf'] = &$conf;
		$this->data['userPerm'] = &$this->userperm;
		$this->data['status'] = &$this->session->userdata('status');
		$this->session->unset_userdata('status');
		//
	}

//ADD
	function add()
	{
	
	//What we need
		$id=$this->uri->segment(4);

	//Check
		if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'rw', $id) ){
			redirect('page/'.$id);
			die();
		}

		if( !$this->menu->isExists($id) )
			redirect('page/'.$id);		
	//Do
		if( $this->input->post('mid') && $this->input->post('title') ){
			if( $this->menu->isExists($this->input->post('mid')) ){
				$this->page->data = $_POST;
				$this->page->insert_entry();
				$this->session->set_flashdata('status', 'Новая страница: "'.$this->input->post('title').'" добавлена');
			}
			redirect('page/'.$id);
		}
		
	//Colecting
		//Menu
		$this->data['menu'] = $this->menu->get($id); 
		$this->data['catInfo'] = $this->menu->catInfo;
		//
		$config    = array('name' => 'body', 'content' => '');
		$this->load->library('spaw', $config);
		
	//View
		$this->data['type'] = 'admin/addPage';
		$this->load->view('main',$this->data);
	}
	
//EDIT
	function edit()
	{
		
	//What we need	
		$id=$this->uri->segment(4);
		$this->page->var['id'] = $id;
		
	//Check Perm
		if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'re', '', $id) ){
			redirect('page/'.$id);
			die();
		}
		
	//Do
		if(isset($_POST['mid'])&&isset($_POST['title'])){
			if( $this->menu->isExists($this->input->post('mid')) ){
				$this->page->data = $_POST;
				$this->page->update_entry();
				$this->session->set_flashdata('status', 'Страница: "'.$this->input->post('title').'" измененна');
			}
			redirect('page/'.$_POST['mid']);
		}
		
	//Collecting
		$this->data['menu'] = $this->menu->get();
		//Page Data
		$page = $this->page->get();

		$this->data['page'] = $page;
		$config    = array('name' => 'body');
		if(isset($page[0]->body)) $config['content'] = $page[0]->body; else $config['content'] = '';
		$this->load->library('spaw', $config);
		//
		
	//View
		$this->data['type'] = 'admin/editPage';
		$this->load->view('main',$this->data);
	}

//REMOVE
	function remove()
	{
		//Page remove
		if(isset($_POST['pageId'])){
			//CheckPerm
			if( !$this->userperm->checkPerm( $this->session->userdata('user_id'),'rw', '', $_POST['pageId']) ){
				die('Не достаточно прав');
			}
			
			//What we need
			$this->page->var['id'] = $_POST['pageId'];
			
			//Do
			if($this->page->remove_entry())
				$return =  'Страница удаленна';
			else
				$return =  'Ошибка';
			//View
				echo $return;
		}
		//
	}
}

/* End of file main.php */
/* Location: ./system/application/controllers/main.php */
